package macrotools;
import neko.FileSystem;
import neko.io.File;
import macrotools.Imports;

/**
 * ...
 * @author Waneck
 */

class EContext 
{

	/**
	 * Resolves a type by its path in the macro context.
	 * thanks for Heinz Hölzer for how to do this
	 */
	public static function resolveClass(path:String):Class<Dynamic>
	{
		var hash = Context.signature(path);
		var name = "Helper__" + hash;
		var txt = new StringBuf();
		txt.add('package;\n\n@:macro class Importer__');
		txt.add(hash);
		txt.add('{\npublic static function build():haxe.macro.Type {\n\t');
		txt.add(path);
		txt.add(';\n\treturn haxe.macro.Context.getType("String"); } }\n\n');
		txt.add('typedef ');
		txt.add(name);
		txt.add(' = haxe.macro.MacroType<(Importer__');
		txt.add(hash);
		txt.add('.build())>;');
		
		var f = File.write(name + ".hx", false);
		f.writeString(txt.toString());
		f.close();
		
		Context.getType(name);
		FileSystem.deleteFile(name + ".hx");
		return Type.resolveClass(path);
	}
	
}